using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.Collections.Generic;
using UndergroundSurvival.Rooms;
using UndergroundSurvival.Managers;

namespace UndergroundSurvival.UI
{
    public class RoomUIController : MonoBehaviour
    {
        [Header("Room Info Panel")]
        [SerializeField] private GameObject roomInfoPanel;
        [SerializeField] private TextMeshProUGUI roomNameText;
        [SerializeField] private TextMeshProUGUI roomStatusText;
        [SerializeField] private TextMeshProUGUI roomDescriptionText;
        [SerializeField] private Image roomIcon;
        [SerializeField] private Slider healthSlider;
        [SerializeField] private Slider efficiencySlider;

        [Header("Resource Panel")]
        [SerializeField] private GameObject resourcePanel;
        [SerializeField] private Transform resourceListContent;
        [SerializeField] private GameObject resourceItemPrefab;

        [Header("Action Buttons")]
        [SerializeField] private Button upgradeButton;
        [SerializeField] private Button repairButton;
        [SerializeField] private Button demolishButton;
        [SerializeField] private Button closeButton;

        private Room currentRoom;
        private List<GameObject> resourceItems = new List<GameObject>();

        private void Start()
        {
            InitializeButtons();
            HideAllPanels();
        }

        private void InitializeButtons()
        {
            if (upgradeButton != null)
                upgradeButton.onClick.AddListener(OnUpgradeClicked);
            if (repairButton != null)
                repairButton.onClick.AddListener(OnRepairClicked);
            if (demolishButton != null)
                demolishButton.onClick.AddListener(OnDemolishClicked);
            if (closeButton != null)
                closeButton.onClick.AddListener(HideAllPanels);
        }

        public void ShowRoomInfo(Room room)
        {
            if (room == null) return;

            currentRoom = room;
            UpdateRoomInfo();
            ShowAllPanels();
        }

        private void UpdateRoomInfo()
        {
            if (currentRoom == null) return;

            // 更新基本信息
            roomNameText.text = currentRoom.roomName;
            roomStatusText.text = currentRoom.isBuilt ? "已建造" : "建造中";
            roomDescriptionText.text = currentRoom.description;
            roomIcon.sprite = currentRoom.icon;

            // 更新状态条
            healthSlider.value = currentRoom.health / currentRoom.maxHealth;
            efficiencySlider.value = currentRoom.efficiency;

            // 更新资源列表
            UpdateResourceList();

            // 更新按钮状态
            UpdateButtonStates();
        }

        private void UpdateResourceList()
        {
            // 清除现有资源项
            foreach (var item in resourceItems)
            {
                Destroy(item);
            }
            resourceItems.Clear();

            if (currentRoom == null) return;

            // 添加资源消耗项
            if (currentRoom is BaseRoom baseRoom)
            {
                if (baseRoom.consumptionInputs != null)
                {
                    foreach (var input in baseRoom.consumptionInputs)
                    {
                        CreateResourceItem(input.resourceName, input.amount, true);
                    }
                }

                if (baseRoom.productionOutputs != null)
                {
                    foreach (var output in baseRoom.productionOutputs)
                    {
                        CreateResourceItem(output.resourceName, output.amount, false);
                    }
                }
            }
        }

        private void CreateResourceItem(string resourceName, float amount, bool isInput)
        {
            GameObject item = Instantiate(resourceItemPrefab, resourceListContent);
            TextMeshProUGUI itemText = item.GetComponentInChildren<TextMeshProUGUI>();
            if (itemText != null)
            {
                string prefix = isInput ? "-" : "+";
                itemText.text = $"{prefix} {resourceName}: {amount}";
            }
            resourceItems.Add(item);
        }

        private void UpdateButtonStates()
        {
            if (currentRoom == null) return;

            bool isBuilt = currentRoom.isBuilt;
            bool canUpgrade = isBuilt && currentRoom.level < currentRoom.maxLevel;
            bool needsRepair = isBuilt && currentRoom.health < currentRoom.maxHealth;

            if (upgradeButton != null)
            {
                upgradeButton.interactable = canUpgrade && 
                    RoomPrefabManager.Instance.HasEnoughResources(currentRoom.roomName, true);
            }

            if (repairButton != null)
            {
                repairButton.interactable = needsRepair;
            }

            if (demolishButton != null)
            {
                demolishButton.interactable = isBuilt;
            }
        }

        private void OnUpgradeClicked()
        {
            if (currentRoom == null) return;

            if (RoomPrefabManager.Instance.HasEnoughResources(currentRoom.roomName, true))
            {
                RoomPrefabManager.Instance.ConsumeBuildResources(currentRoom.roomName, true);
                currentRoom.Upgrade();
                UpdateRoomInfo();
            }
        }

        private void OnRepairClicked()
        {
            if (currentRoom == null) return;

            currentRoom.Repair();
            UpdateRoomInfo();
        }

        private void OnDemolishClicked()
        {
            if (currentRoom == null) return;

            currentRoom.Demolish();
            HideAllPanels();
        }

        private void ShowAllPanels()
        {
            if (roomInfoPanel != null)
                roomInfoPanel.SetActive(true);
            if (resourcePanel != null)
                resourcePanel.SetActive(true);
        }

        private void HideAllPanels()
        {
            if (roomInfoPanel != null)
                roomInfoPanel.SetActive(false);
            if (resourcePanel != null)
                resourcePanel.SetActive(false);
            currentRoom = null;
        }
    }
} 